from nativePlugins.fuckAdvertising.fuckAdvertising import *
from nativePlugins.fuckAdvertising.fuckAdvertising import hideElementCSS, hideElementJS

class fuckAdvertisingSpecific(fuckAdvertisingFilter):
    def __init__(self):
        super()

    def allowRequest(self, netinfo):
        url = netinfo.getURL()
        domain = netinfo.getDomain()
        # fuckAdvertising: fuckOff.
        if str(url).__contains__("www.googletagmanager.com") or \
                str(url).__contains__("static.hotjar.com") or \
                str(url).__contains__("smartadserver.com") or \
                str(url).__contains__("adform.net") or \
                str(url).__contains__("meetrics.net") or \
                str(url).__contains__("gemius.pl") or \
                str(url).__contains__("pushpushgo.com") or \
                str(url).__contains__("ads.pubmatic.com") or \
                str(url).__contains__("mxcdn.net") or \
                str(url).__contains__("connectad.io") or \
                str(url).__contains__("openx.net") or \
                str(url).__contains__("nsaudience.pl") or \
                str(url).__contains__("play.google.com/log") or \
                str(url).__contains__("www.startpage.com/js/abp.js"):
            return False
        # fuckAdvertising: AntiWP. - Easily one of the worst sites known to man.
        if str(url).startswith("https://www.wp.pl") and not str(url).endswith("==/") and not str(url).endswith("=/") \
                and not str(url).endswith("www.wp.pl/"):
            return False
        # fuckAdvertising: Interia Fixer.
        if (str(url).__contains__("www.interia.pl") and str(url).__contains__("-sponsored-")) \
                or str(url).__contains__("nieruchomosci.interia.pl") \
                or (str(url).__contains__("www.interia.pl") and str(url).endswith("kupony.js")) \
                or str(url).__contains__("iwa.hit.interia.pl/1/hit.t") \
                or str(url).__contains__("interia.gazetkapromocyjna.pl"):
            return False
        # fuckAdvertising: noZucc.
        if str(url).__contains__("connect.facebook.net/en_US/fbevents.js") \
                or str(url).__contains__("staticxx.facebook.com/connect/xd_arbiter/") \
                or str(url).__contains__("connect.facebook.net/en_US/sdk.js"):
            return False
        # fuckAdvertising: fuckOffReddit.
        if str(url).__contains__("gateway.reddit.com/desktopapi/v1/sidebar_ads") \
                or str(url).__contains__("www.redditstatic.com/desktop2x/js/ads.js") \
                or str(url).__contains__("c.amazon-adsystem.com") or str(url).__contains__("c.aaxads.com/aax.js") \
                or str(url).__contains__("www.redditstatic.com/moat/moatframe.js") \
                or str(url).__contains__("www.redditmedia.com/moat/proxy"):
            return False
        # fuckAdvertising: fuckOffTwitch.
        if str(url).__contains__("algolianet.com") or str(url).__contains__("countess.twitch.tv/ping.gif") \
                or str(url).__contains__("gql.twitch.tv/gql"):
            return False
        # fuckAdvertising: Other
        if str(url).__contains__("improving.duckduckgo.com/t/l")\
                or str(url).__contains__("improving.duckduckgo.com/t/hi")\
                or str(url).__contains__("reddit.com/api/request_promo") \
                or str(url).__contains__("startpage.com/sp/adsense/search/") \
                or str(url).__contains__("startpage.com/sp/afs/ads/i/"):
            return False
        # fuckAdvertising: NoGoogle
        if (str(url).__contains__("/pagead/1p-user-list/") and str(url).startswith("https://www.google"))\
                or (str(url).__contains__("/pagead/lvz") and str(url).startswith("https://www.google"))\
                or (str(url).startswith("https://www.youtube.com/embed/") and
                    str(url).__contains__("origin=https%3A%2F%2Fwww.youtube.com&widgetid"))\
                or str(url).__contains__("adclick.g.doubleclick.net/")\
                or str(url).__contains__("api.github.com/_private/browser/stats"):
            return False
        # fuckAdvertising: Spotify
        if str(url).__contains__("open.scdn.co/cdn/build/web-player/xpui-desktop-routes-homepage-takeover")\
                or str(url).__contains__("cdn.cookielaw.org/consent/")\
                or str(url).__contains__("geolocation.onetrust.com/cookieconsentpub/")\
                or str(url).__contains__("gew-spclient.spotify.com/gabo-receiver-service/v3/events")\
                or str(url).__contains__("gew-spclient.spotify.com/melody/v1/msg/jssdk_playback_start")\
                or str(url).__contains__("gew-spclient.spotify.com/melody/v1/msg/track_stream_verification")\
                or str(url).__contains__("gew-spclient.spotify.com/melody/v1/msg/jssdk_playback_stats"):
            return False
        return True

    def getName(self):
        return "fuckAdvertising: Specifics"

    def pageLoaded(self, event):
        url = event.getURL()
        print("pageLoaded: " + str(event.source) + ", " + str(event.getURL()))
        if str(url).startswith("https://www.youtube.com"):
            PAYLOAD = ["ytd-video-masthead-ad-v3-renderer", "#masthead-ad"]
            for payload in PAYLOAD:
                p = hideElementJS(payload)
                event.source.runJavaScript(p)
        if str(url).__contains__("old.reddit.com"):
            PAYLOAD = [".premium-banner", ".infobar.listingsignupbar",
                       ".link.promotedlink.promoted, .link.promotedlink.external", ".organic-listing .promoted"]
            for payload in PAYLOAD:
                p = hideElementJS(payload)
                event.source.runJavaScript(p)

    def pageLoadStart(self, event):
        if True:
            pass
        url = event.getURL()
        print("pageLoadStart: " + str(event.source) + ", " + str(event.getURL()))
        """if str(url).__contains__("old.reddit.com"):
            PAYLOAD = [".premium-banner", ".infobar.listingsignupbar",
                       ".link.promotedlink.promoted, .link.promotedlink.external", ".organic-listing .promoted"]
            for payload in PAYLOAD:
                p = hideElementJS(payload)
                event.source.runJavaScript(p)"""

    def pageLoadedPercent(self, event, percent):
        if percent >= 80 and str(event.getURL()).__contains__("old.reddit.com"):
            PAYLOAD = [".premium-banner", ".infobar.listingsignupbar",
                       ".link.promotedlink.promoted, .link.promotedlink.external", ".organic-listing .promoted"]
            for payload in PAYLOAD:
                p = hideElementJS(payload)
                event.source.runJavaScript(p)
        if percent >= 81 and str(event.getURL()).startswith("https://www.youtube.com"):
            PAYLOAD = ["ytd-video-masthead-ad-v3-renderer", "#masthead-ad"]
            for payload in PAYLOAD:
                p = hideElementJS(payload)
                event.source.runJavaScript(p)


fuckAdvertisingSpecific_INSTANCE = fuckAdvertisingSpecific()